/**
 * File		: Layer.java
 * Subject	: Creates a layer for matrix transformation
 * Author	: Isaac Parenteau (locus2k@gmail.com)
 * Date		: April 25th, 2011
 * Note		: Code borrowed from Google and modified to fit the project 
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package src.project.cube;

import src.project.util.Matrix;

public class Layer {
	
	public static final int xAxis = 0;
	public static final int yAxis = 1;
	public static final int zAxis = 2;
	
	private int axis;
	
	public  Shape[] Shapes = new Shape[9];
	private Matrix transform = new Matrix();
	
	public Layer(int axis){
		this.axis = axis;
		transform.setIdentity();
	}
	
	public void startAnimation(){
		for(int i = 0; i < Shapes.length; i++){
			Shape shape = Shapes[i];
			if(shape != null)
				shape.startAnimation();
		}
	}
	
	public void endAnimation(){
		for(int i = 0; i < Shapes.length; i++){
			Shape shape = Shapes[i];
			if(shape != null)
				shape.endAnimation();
		}
	}
	
	public void setAngle(float angle){
		
		//normalize the angle
		float twopi = (float)Math.PI * 2f;
		while(angle >= twopi) angle -= twopi;
		while(angle < 0f) angle += twopi;
		
		float sin = (float)Math.sin(angle);
		float cos = (float)Math.cos(angle);
		
		float[][] matrix = transform.Matrix;
		
		switch(axis){
		case xAxis:
			matrix[1][1] = cos;
			matrix[1][2] = sin;
			matrix[2][1] = -sin;
			matrix[2][2] = cos;
			matrix[0][0] = 1f;
			matrix[0][1] = matrix[0][2] = matrix[1][0] = matrix[2][0] = 0f;
			break;
		case yAxis:
			matrix[0][0] = cos;
			matrix[0][2] = sin;
			matrix[2][0] = -sin;
			matrix[2][2] = cos;
			matrix[1][1] = 1f;
			matrix[0][1] = matrix[1][0] = matrix[1][2] = matrix[2][1] = 0f;
			break;
		case zAxis:
			matrix[0][0] = cos;
			matrix[0][1] = sin;
			matrix[1][0] = -sin;
			matrix[1][1] = cos;
			matrix[2][2] = 1f;
			matrix[2][0] = matrix[2][1] = matrix[0][2] = matrix[1][2] = 0f;
			break;
		}
		
		for(int i = 0; i < Shapes.length; i++){
			Shape shape = Shapes[i];
			if(shape != null)
				shape.animateTransform(transform);
		}
		
	}

}
